package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import com.attendance.www.hr.beans.TipsBean;
import com.attendance.www.hr.dao.mapper.AttendanceMapper;
import com.attendance.www.hr.dao.mapper.EmployeeRoleMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.dao.mapper.TipsExtMapper;
import com.attendance.www.hr.entitys.Attendance;
import com.attendance.www.hr.entitys.AttendanceExample;
import com.attendance.www.hr.entitys.EmployeeRole;
import com.attendance.www.hr.entitys.EmployeeRoleExample;
import com.attendance.www.hr.service.ITipsService;

@Service
public class TipsServiceImpl implements ITipsService {

	@Resource
	private TipsExtMapper tipsDao;
	@Resource
	private EmployeeRoleMapper employeeRoleDao;
	@Resource
	private EmployeeSchedulMapper employeeSchedulDao;
	@Resource
	private SchedulDayMapper schedulDayMapper;
	@Resource
	private AttendanceMapper attendanceMapper;

	/**
	 * 获取待审批个数信息--zx
	 */
	@Override
	public TipsBean getTips(Integer employeeId) {
		EmployeeRoleExample erExample = new EmployeeRoleExample();
		erExample.or().andEmployeeIdEqualTo(employeeId).andFromDateLessThanOrEqualTo(new Date())
				.andThruDateGreaterThanOrEqualTo(new Date());
		erExample.or().andEmployeeIdEqualTo(employeeId).andFromDateLessThanOrEqualTo(new Date()).andThruDateIsNull();

		List<EmployeeRole> erList = employeeRoleDao.selectByExample(erExample);
		List<Integer> roleIdList = new ArrayList<Integer>();
		if (erList.size() > 0) {
			for (EmployeeRole employeeRole : erList) {
				roleIdList.add(employeeRole.getRoleId());
			}
		}

		TipsBean tipsBean = new TipsBean();
		tipsBean.setBusinessTravelNum(tipsDao.getBusinessTravelNum(roleIdList));
		tipsBean.setFurloughNum(tipsDao.getFurloughNum(roleIdList));
		tipsBean.setGoOutNum(tipsDao.getGoOutNum(roleIdList));
		tipsBean.setOverTimeNum(tipsDao.getOverTimeNum(roleIdList));
		tipsBean.setRetroactiveNum(tipsDao.getRetroactiveNum(roleIdList));
		Integer total = tipsBean.getBusinessTravelNum() + tipsBean.getFurloughNum() + tipsBean.getGoOutNum()
				+ tipsBean.getOverTimeNum() + tipsBean.getRetroactiveNum();
		tipsBean.setTotal(total);
		return tipsBean;
	}

	/**
	 * 判断是否是休息日
	 */
	@Override
	public TipsBean getAttendanceTime(Date attendanceDate, Integer employeeId) {
		Integer isRestDay = 1;// 1是休息日，0是工作日
		// 日期查询考勤信息
		/*
		 * EmployeeSchedulExample employeeSchedulExample=new EmployeeSchedulExample();
		 * employeeSchedulExample.createCriteria().andEmployeeIdEqualTo(employeeId)
		 * .andFromDateLessThanOrEqualTo(attendanceDate).andThruDateGreaterThanOrEqualTo
		 * (attendanceDate); employeeSchedulExample.setOrderByClause("sw_id desc");
		 * List<EmployeeSchedul>
		 * esList=employeeSchedulDao.selectByExample(employeeSchedulExample); if
		 * (esList.size()>0) { EmployeeSchedul employeeSchedul=esList.get(0); Integer
		 * swId=employeeSchedul.getSwId(); SchedulDayExample schedulDayExample=new
		 * SchedulDayExample(); schedulDayExample.createCriteria().andSwIdEqualTo(swId)
		 * .andSdDateEqualTo(attendanceDate); List<SchedulDayKey>
		 * sdList=schedulDayMapper.selectByExample(schedulDayExample); if
		 * (sdList.size()>0) { isRestDay=0; } }
		 */
		AttendanceExample attendanceExample = new AttendanceExample();
		attendanceExample.createCriteria().andEmployeeIdEqualTo(employeeId).andAttendanceDateEqualTo(attendanceDate);
		List<Attendance> attendanceList = attendanceMapper.selectByExample(attendanceExample);
		if (attendanceList.size() > 0) {
			Integer attendanceState = attendanceList.get(0).getAttendanceState();
			if(StringUtils.isNotBlank(attendanceState+"")) {
				if (attendanceState != 9) {
					isRestDay = 0;
				}
			}
		}
		TipsBean tipsBean = new TipsBean();
		tipsBean.setIsRestDay(isRestDay);
		return tipsBean;
	}
}
